<?php

namespace App\Services\Patent;

use App\Extensions\EGrid;
use App\Extensions\Renderable\PatentAgencyDetailRenderAble;
use App\Extensions\Renderable\PatentAssessDetailRenderAble;
use App\Services\ColumnService;
use App\Services\SettingService;
use App\Setting;
use Encore\Admin\Grid;

class PatentApplyColumnService extends ColumnService
{

    public function uuid(Grid $grid)
    {
        if ($grid instanceof EGrid) {
            $grid->column('uuid', __('登记编号'))->modalLink('详情', function ($model) {
                return admin_route('patentApplies.show', [$model['id']]);
            });
        }
    }

    public function patent_name(Grid $grid)
    {
        $grid->column('patent_name', __('专利名称'));
    }

    public function patent_type(Grid $grid)
    {
        $grid->column('patent_type_id', __('专利类型'))->using(patent_type_select_option())->width(80);
    }

    public function charge_person(Grid $grid)
    {
        $grid->column('charge_person', __('第一发明人'));
    }

    public function department_name(Grid $grid)
    {
        $grid->model()->with(['department']);
        $grid->column('department.name', __('dept_name'));
    }

    public function agency(Grid $grid)
    {
        $grid->model()->with(['agency']);
        $grid->column('agency.company', __('意向代理机构'))->modal('详情', PatentAgencyDetailRenderAble::class);
    }

    public function file(Grid $grid)
    {
        $grid->column('file', '交底书')->display(function ($value) {
            return $value ? '下载' : '';
        })->link(function ($model) {
            return file_custom_preview_url($model['file'], '(交底书)' . $model['patent_name']);
        });
    }

    public function score(Grid $grid, $role = 0)
    {
        if (config('settings.module.expert_module')) {
            if ($role == 1 && $grid instanceof EGrid) {
                $grid->column('score', '综合评分')->display(function ($score) {
                    return $score > 0 ? $score : '';
                })->layerShow(function ($model) {
                    return admin_route('patent-assesses.index', ['patent_apply_id' => $model['id']]);
                });
            } else {
                $grid->column('score', '综合评分')->display(function ($score) {
                    return $score > 0 ? $score : '';
                })->modal('详情', PatentAssessDetailRenderAble::class);
            }
        }
    }

    public function score_template(Grid $grid, $role = 0)
    {
        if (config('settings.module.expert_module')) {
            if (in_array($role, [1, 3])) {
                $grid->column('score_template', '评分指标')->display(function () {
                    return SettingService::patentPfTemplateATag();
                });
            }
        }
    }

    public function pg_table(Grid $grid)
    {
        if (config('settings.module.expert_module')) {
            $grid->column('table', '评估表')->display(function () {
                return '下载';
            })->link(function ($model) {
                return admin_route('files.patent-apply.table', [$model['id'], 'token' => $model->token()]);
            });
        }
    }

    public function retrieval_report(Grid $grid)
    {
        $grid->column('retrieval_report', '检索报告')->display(function ($value) {
            return $value ? '查看' : '';
        })->link(function ($model) {
            return file_custom_preview_url($model['retrieval_report'], '(检索报告)' . $model['patent_name'], 'show');
        });
    }

    public function cert(Grid $grid)
    {
        $grid->column('cert', '专利证书')->display(function ($value) {
            return $value ? '下载' : '';
        })->link(function ($model) {
            return file_custom_preview_url($model['cert'], '(证书)' . $model['patent_name']);
        });
    }

    public function review_status(Grid $grid)
    {
        $admin_review_name = Setting::getReviewName(1);
        $dept_review_name = Setting::getReviewName(2);
        $grid->column('review_status', __('审批状态'))->display(function ($value) use ($admin_review_name, $dept_review_name) {
            if ($value) {
                return data_get([
                    1 => '<span class="label label-success">' . $admin_review_name . '-通过</span>',
                    2 => '<span class="label label-default">' . $admin_review_name . '-未通过</span>',
                    3 => '<span class="label label-default">' . $admin_review_name . '-退回修改</span>',
                ], $value);
            } else {
                return data_get([
                    '<span class="label label-warning">' . $dept_review_name . '-待处理</span>',
                    '<span class="label label-warning">' . $admin_review_name . '-待处理</span>',
                    '<span class="label label-default">' . $dept_review_name . '-未通过</span>',
                ], $this->dept_review_status);
            }
        });
    }

}
